﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Simulation.Framework
{
    public class Helper
    {
        /// <summary>
        /// 播放动画
        /// </summary>
        public static void PlayAnimator(GameObject go,string stateName,UnityAction onComplete = null) {
            Animator animator = go.GetComponent<Animator>();
            if (animator == null) {
                onComplete?.Invoke();
                return;
            }
            animator.enabled = true;
            animator.speed = 1;
            //animator.Rebind();
            animator.Play(stateName,0);

            AnimationClip animationClip = Array.Find(animator.runtimeAnimatorController.animationClips, v => v.name == stateName);
            if (animationClip == null) {
                onComplete?.Invoke();
                return;
            }

            Timer timer = new Timer(animationClip.length);
            timer.id = stateName;
            timer.complete = onComplete;
            timer.type = ETimerType.FIXED;
            SingleBehavior<TimerManager>.getInstance().AddTimer(timer);
        }

        /// <summary>
        /// 暂停动画
        /// </summary>
        public static void PauseAnimator(GameObject go,string stateName = "") {
            Animator animator = go.GetComponent<Animator>();
            if (animator == null) return;
            animator.speed = 0;

            Timer timer = SingleBehavior<TimerManager>.getInstance().GetTimer(stateName);
            SingleBehavior<TimerManager>.getInstance().Pause(timer);
        }

        /// <summary>
        /// 继续动画
        /// </summary>
        public static void UnPauseAnimator(GameObject go,string stateName = "") {
            Animator animator = go.GetComponent<Animator>();
            if (animator == null) return;
            animator.speed = 1;

            Timer timer = SingleBehavior<TimerManager>.getInstance().GetTimer(stateName);
            SingleBehavior<TimerManager>.getInstance().UnPause(timer);
        }

        /// <summary>
        /// 停止动画
        /// </summary>
        public static void StopAnimator(GameObject go,string stateName) {
            Animator animator = go.GetComponent<Animator>();
            if (animator == null) return;
            animator.Rebind();
            animator.enabled = false;
            Timer timer = SingleBehavior<TimerManager>.getInstance().GetTimer(stateName);
            SingleBehavior<TimerManager>.getInstance().RemoveTimer(timer);
        }

        /// <summary>
        /// 停止动画
        /// </summary>
        public static void StopAnimator(GameObject go)
        {
            Animator animator = go.GetComponent<Animator>();
            if (animator == null) return;
            animator.enabled = false;

            AnimationClip[] clips = animator.runtimeAnimatorController.animationClips;
            for (int i = 0; i < clips.Length; i++)
            {
                string stateName = clips[i].name;
                Timer timer = SingleBehavior<TimerManager>.getInstance().GetTimer(stateName);
                SingleBehavior<TimerManager>.getInstance().RemoveTimer(timer);
            }
        }

        /// <summary>
        /// 获取 streaming Asset 路径
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        public static string GetStreamingAssets()
        {
            string url = Application.streamingAssetsPath;
#if UNITY_EDITOR_OSX
            url = "file://" + url;
#endif
            return url;
        }
    }
}